package net.hn.hnms.biz.cockpit.service.impl;

import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.cockpit.domain.vo.DistanceCorrelationVo;
import net.hn.hnms.biz.cockpit.domain.vo.DistanceTrendVo;
import net.hn.hnms.biz.cockpit.domain.vo.OptionVo;
import net.hn.hnms.biz.cockpit.domain.vo.ProductTrendVo;
import net.hn.hnms.biz.cockpit.mapper.DispatchCockpitMapper;
import net.hn.hnms.biz.cockpit.service.IDispatchCockpitService;
import net.hn.hnms.biz.cockpit.service.IProductionManagementService;
import net.hn.hnms.biz.cockpit.utils.OweDateUtils;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Service
@RequiredArgsConstructor
public class DispatchCockpitServiceImpl implements IDispatchCockpitService {

    private final DispatchCockpitMapper dispatchCockpitMapper;

    private final IProductionManagementService productionManagementService;

    @Override
    public JSONObject getProductTrend(String mineCode) {
        JSONObject result = new JSONObject();
        result.set("legendData", Arrays.asList("计划", "实际"));
        result.set("unit", "万吨");
        String endDate = DateUtils.getDate();
        String beginDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(DateUtils.getNowDate(), -30));
        List<String> xData = OweDateUtils.getXData(beginDate, endDate, "day");
        xData.remove(xData.size() - 1);
        //List<ProductTrendVo> productTrendList = dispatchCockpitMapper.getProductTrend(beginDate, endDate, mineCode);
        List<ProductTrendVo> productTrendList = productionManagementService.getProductTrend(xData,beginDate, endDate, mineCode);
        List<String> planProductList = Lists.newArrayList();
        List<String> actualProductList = Lists.newArrayList();

        xData.forEach(x -> {
            ProductTrendVo productTrendVo = productTrendList.stream().filter(item -> item.getProductDate().equals(x)).findFirst().orElse(null);
            planProductList.add(productTrendVo == null ? "0" : productTrendVo.getPlanProduct());
            actualProductList.add(productTrendVo == null ? "0" : productTrendVo.getActualProduct());
        });
        result.set("xData", xData);
        result.set("yData", Arrays.asList(planProductList, actualProductList));
        return result;
    }

    @Override
    public JSONObject getDistanceTrend(String mineCode) {
        JSONObject result = new JSONObject();
        result.set("legendData", Arrays.asList("计划", "实际"));
        result.set("unit", "米");
        String endDate = DateUtils.getDate();
        String beginDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(DateUtils.getNowDate(), -30));
        List<String> xData = OweDateUtils.getXData(beginDate, endDate, "day");
        xData.remove(xData.size() - 1);
        //List<DistanceTrendVo> distanceTrendList = dispatchCockpitMapper.getDistanceTrend(beginDate, endDate, mineCode);
        List<DistanceTrendVo> distanceTrendList = productionManagementService.getDistanceTrend(xData,beginDate, endDate, mineCode);
        List<String> plan = Lists.newArrayList();
        List<String> actual = Lists.newArrayList();
        xData.forEach(x -> {
            DistanceTrendVo distanceTrendVo = distanceTrendList.stream().filter(item -> item.getDistanceDate().equals(x)).findFirst().orElse(null);
            plan.add(distanceTrendVo == null ? "0" : distanceTrendVo.getPlanDistance());
            actual.add(distanceTrendVo == null ? "0" : distanceTrendVo.getActualDistance());
        });
        result.set("xData", xData);
        result.set("yData", Arrays.asList(plan, actual));
        return result;
    }

    @Override
    public List<OptionVo> getWorkFaceOutput(String mineCode) {
        return dispatchCockpitMapper.getDispatchCockpitMapper(mineCode);
    }

    @Override
    public List<String> getMineLeader(String mineCode) {
        String mineLeader = dispatchCockpitMapper.getMineLeader(mineCode);
        return StringUtils.isBlank(mineLeader) ? Lists.newArrayList() : Arrays.asList(mineLeader.split(","));
    }

    @Override
    public JSONObject getOutputCorrelation(String mineCode, String type) {
        JSONObject result = new JSONObject();
        String curDate = "";
        String lastDate = "";
        if ("month".equals(type)) {
            curDate = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
            // 去年的本月
            lastDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM, DateUtils.addYears(DateUtils.getNowDate(), -1));
        } else if ("year".equals(type)) {
            curDate = DateUtils.dateTimeNow(DateUtils.YYYY);
            lastDate = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.addYears(DateUtils.getNowDate(), -1));
        }
        result.set("legendData", Arrays.asList(lastDate, curDate));
        result.set("unit", "万吨");
        ProductTrendVo lastProduct = dispatchCockpitMapper.getOutputCorrelation(mineCode, lastDate, type);
        ProductTrendVo curProduct = dispatchCockpitMapper.getOutputCorrelation(mineCode, curDate, type);
        List<String> xData = Arrays.asList("计划", "实际");
        result.set("xData", xData);
        result.set("yData", Arrays.asList(Arrays.asList(lastProduct.getPlanProduct(), curProduct.getPlanProduct()), Arrays.asList(lastProduct.getActualProduct(), curProduct.getActualProduct())));
        return result;
    }

    @Override
    public JSONObject getDistanceCorrelation(String mineCode, String type) {
        JSONObject result = new JSONObject(true);
        String curDate = "";
        String lastDate = "";
        if ("month".equals(type)) {
            curDate = DateUtils.dateTimeNow(DateUtils.YYYY_MM);
            // 去年的本月
            lastDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM, DateUtils.addYears(DateUtils.getNowDate(), -1));
        } else if ("year".equals(type)) {
            curDate = DateUtils.dateTimeNow(DateUtils.YYYY);
            lastDate = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.addYears(DateUtils.getNowDate(), -1));
        }
        List<DistanceCorrelationVo> lastDistanceCorrelationList = dispatchCockpitMapper.getDistanceCorrelation(mineCode, lastDate, type);
        List<DistanceCorrelationVo> curDistanceCorrelationList = dispatchCockpitMapper.getDistanceCorrelation(mineCode, curDate, type);
        JSONObject lastPie = new JSONObject(true);
        lastPie.set("day", lastDate);
        lastPie.set("data", lastDistanceCorrelationList);
        JSONObject curPie = new JSONObject(true);
        curPie.set("day", curDate);
        curPie.set("data", curDistanceCorrelationList);
        result.set("pies", Arrays.asList(lastPie, curPie));
        DistanceCorrelationVo last = new DistanceCorrelationVo();
        last.setDistanceDate(lastDate);
        DistanceCorrelationVo cur = new DistanceCorrelationVo();
        cur.setDistanceDate(curDate);
        if (CollectionUtils.isNotEmpty(lastDistanceCorrelationList)) {
            last.setPlanDistance(lastDistanceCorrelationList.stream().mapToDouble(DistanceCorrelationVo::getPlanDistance).sum() );
            last.setActualDistance(lastDistanceCorrelationList.stream().mapToDouble(DistanceCorrelationVo::getActualDistance).sum());
        }
        if (CollectionUtils.isNotEmpty(curDistanceCorrelationList)) {
            cur.setPlanDistance(curDistanceCorrelationList.stream().mapToDouble(DistanceCorrelationVo::getPlanDistance).sum());
            cur.setActualDistance(curDistanceCorrelationList.stream().mapToDouble(DistanceCorrelationVo::getActualDistance).sum() );
        }
        result.set("histogram", Arrays.asList(last, cur));
        return result;
    }


}
